package com.lz.sports.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.lz.library.adapter.SimpleAdapter
import com.lz.library.adapter.SimpleViewHolder
import com.lz.library.extension.APP
import com.lz.library.extension.getScreenPoint
import com.github.mjdev.libaums.fs.UsbFile
import com.lz.sports.R
import com.lz.sports.storage.UsbStorageDevice

class UsbFileDialog(@NonNull context: Context, private val listFile: List<UsbFile>) :
    Dialog(context, com.lz.library.R.style.lib_dialogTheme) {

    var onItemClickListener: OnItemClickListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_usb_file)
        this.window!!.setBackgroundDrawableResource(com.lz.library.R.drawable.lib_bg_public_dialog)
        window?.attributes = window?.attributes?.apply {
            val screen = APP.getScreenPoint()
            width = (screen.x * 0.9f).toInt()
        }

        val rv = findViewById<RecyclerView>(R.id.rv_list)
        rv.setHasFixedSize(true)
        val adapter = FileAdapter(listFile)
        rv.adapter = adapter
        adapter.setOnItemClickListener(object : com.lz.library.adapter.BaseAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                dismiss()
                onItemClickListener?.onItemClick(adapter.getItem(position))
            }
        })
    }

    class FileAdapter(listFile: List<UsbFile>) : SimpleAdapter<UsbFile>(listFile) {

        override fun getLayoutResId(viewType: Int): Int = R.layout.item_usb_file

        override fun onBindViewHolder(vh: SimpleViewHolder, position: Int) {
            val item = getItem(position)
            vh.getView<TextView>(R.id.tv_name).text = item.name
            vh.getView<TextView>(R.id.tv_path).text = UsbStorageDevice.usbPath(item)
        }
    }

    interface OnItemClickListener {

        fun onItemClick(usbFile: UsbFile)
    }
}